package com.paas.form.service.impl;


import com.paas.form.entity.FormTaskEtlLogEntity;
import com.paas.form.service.IFormTaskEtlLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paas.form.dao.FormTaskLogDao;
import com.paas.form.entity.FormTaskLogEntity;
import com.paas.form.service.IFormTaskLogService;
import com.paas.common.web.service.BaseServiceImpl;

import java.util.List;


@Service("formTaskLogService")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FormTaskLogServiceImpl  extends BaseServiceImpl<FormTaskLogDao,FormTaskLogEntity> implements IFormTaskLogService{

    private final IFormTaskEtlLogService etlLogService;

    @Override
    public FormTaskLogEntity lastExecution(String jobId) {
        //获取最新一条的任务日志
        return baseMapper.queryLastFormTaskLog(jobId);
    }

    @Override
    public List<FormTaskLogEntity> queryTaskTopJobIds(List<String> jobIds) {
        FormTaskLogEntity taskLogEntity = new FormTaskLogEntity();
        taskLogEntity.setJobIds(jobIds);
        return baseMapper.queryTaskTopJobIds(taskLogEntity);
    }

    @Override
    public FormTaskLogEntity queryLastEtlExecution(String jobId) {
        FormTaskLogEntity taskLogEntity = baseMapper.queryLastEtlExecution(jobId);
        taskLogEntity.setTableTotalNumIssued(0);
        FormTaskEtlLogEntity taskEtlLogEntity = new FormTaskEtlLogEntity();
        taskEtlLogEntity.setJobId(jobId);
        List<FormTaskEtlLogEntity> list = etlLogService.getList(taskEtlLogEntity);
        list.stream().forEach(item->{
            if ("table".equals(item.getDistributionType())){
                Integer tableTotalNumIssued = taskLogEntity.getTableTotalNumIssued();
                tableTotalNumIssued+=item.getFailCount()+item.getSuccessCount();
                taskLogEntity.setTableTotalNumIssued(tableTotalNumIssued);
            }else {
                Integer apiTotalNumIssued = taskLogEntity.getApiTotalNumIssued();
                apiTotalNumIssued+=item.getFailCount()+item.getSuccessCount();
                taskLogEntity.setTableTotalNumIssued(apiTotalNumIssued);
            }
        });
        return taskLogEntity;
    }
}